#!/bin/bash
cd $(dirname $0)
PYTHON_ENV='python3'
APP_PY=$(ls todo_task.py* | tail -1)


function check_python() {
    ${PYTHON_ENV} -c "import tkinter" &>/dev/null || {
        echo "Install python3-tk start"
        sudo apt-get -y install python3-tk
        ${PYTHON_ENV} -c "import tkinter" &>/dev/null || { echo "Install python3-tk failed !"; return 1; }
    }

    ${PYTHON_ENV} -m pip list &>/dev/null || {
        echo "Install python3-pip start"
        sudo apt-get -y install python3-pip
        ${PYTHON_ENV} -m pip list &>/dev/null || { echo "Install python3-pip failed !"; return 1; }
    }

    ${PYTHON_ENV} -c "import schedule" &>/dev/null || {
        echo "Install python3 schedule start"
        python3 -m pip install schedule
        ${PYTHON_ENV} -c "import schedule" &>/dev/null || { echo "Install python3 schedule failed !"; return 1; }
    }

    ${PYTHON_ENV} -c "import playsound" &>/dev/null || {
        echo "Install python3 playsound start"
        python3 -m pip install playsound
        ${PYTHON_ENV} -c "import playsound" &>/dev/null || { echo "Install python3 playsound failed !"; return 1; }
    }
    return 0
}

function check_font() {
    fc-list | grep UKai &>/dev/null || {
        echo "Install UKai font start"
        sudo cp font/ukai.ttc /usr/share/fonts/
        sudo fc-cache -fv
    }
    return 0
}

function start_gui() {
    pgrep -f "${APP_PY}" && { echo "Another ${APP_PY} is running !"; return; }
    ${PYTHON_ENV} "${APP_PY}"
}

function main() {
    check_python  || return
    check_font
    start_gui
}

main
